package ctgu.junitTest;

import static org.junit.jupiter.api.Assertions.*; 
import org.junit.jupiter.api.DisplayName; 
import org.junit.jupiter.api.Test; 
import org.junit.jupiter.api.io.TempDir; 
import java.nio.file.Path;
import java.nio.file.Files; 
import java.util.Arrays; 
import java.io.IOException;

import ctgu.java.Configuration;
import ctgu.java.ValueParseException;
import java.util.Date;
import java.text.*;

public class ConfigurationTest{
	@TempDir
	static Path tempDir;
	@Test
	@DisplayName("当键存在时应该返回不同类型键值")
	void value_should_exit_when_key_exist() throws IOException{
		// arrange
		Path path = tempDir.resolve("app.conf");
		Files.write(path, Arrays.asList(
				"ea=test",
				"eb=false",
				"ec=1",
				"ed=2017-01-21"
				));
		Configuration configuration = new Configuration();
		configuration.fromFile(path.toFile());
		// act
		String a_value = configuration.getProp("ea");
		boolean b_value = configuration.getBoolean("eb");
		boolean bw_value = configuration.getBooleanWithDefault("eb",true);
		int c_value = configuration.getInt("ec",2);
 
		// assert
		assertEquals("test", a_value);
		assertEquals(false, b_value);
		assertEquals(false, bw_value);
		assertEquals(1, c_value);
		
		try{
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date defultvalue = formatter.parse("2018-02-01");
			Date e_value = formatter.parse("2017-01-21");
			Date value = configuration.getDate("ed",defultvalue);
			assertEquals(e_value, value);
			}catch (ParseException e) {
		} 
	}
 
	
	
	@Test
	@DisplayName("当键不存在返回缺省Boolean值")
	void when_key_not_exist_boolean() throws IOException{
		// arrange
		Path path = tempDir.resolve("app.conf");
		Files.write(path, Arrays.asList("ea=false"));
		Configuration configuration = new Configuration();
		configuration.fromFile(path.toFile());
		// act
		boolean value = configuration.getBooleanWithDefault("notexist",true);
 
		// assert
		assertEquals(true, value);
	}
	
	@Test
	@DisplayName("当键不存在返回缺省int值")
	void when_key_not_exist_int() throws IOException{
		// arrange
		Path path = tempDir.resolve("app.conf");
		Files.write(path, Arrays.asList("ea=1"));
		Configuration configuration = new Configuration();
		configuration.fromFile(path.toFile());
		// act
		int value = configuration.getInt("notexist",2);
 
		// assert
		assertEquals(2, value);
	}
	
	@Test
	@DisplayName("当键不存在返回缺省date值")
	void when_key_not_exist_date() throws IOException{
		// arrange
		Path path = tempDir.resolve("app.conf");
		Files.write(path, Arrays.asList("ea=2017-01-21"));
		Configuration configuration = new Configuration();
		configuration.fromFile(path.toFile());
		// act
		try{
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date e_value = formatter.parse("2018-02-01");
			
			Date value = configuration.getDate("notexist",e_value);
			assertEquals(e_value, value);
			}catch (ParseException e) {
		} 
		
	}
	
	
	@Test
	@DisplayName("当键不存在抛出异常")
	void throw_ValueParseException_when_key_not_exist() throws IOException {
		// arrange
		Path path = tempDir.resolve("app.conf");
		Files.write(path, Arrays.asList("ea=false"));
 
		Configuration conf = new Configuration();
		conf.fromFile(path.toFile());
 
		// act

		Throwable throwable = assertThrows(ValueParseException.class, () ->{
			conf.getBoolean("notexist");});
 
		// assert
		assertAll(
				() -> assertEquals("键值不存在",throwable.getMessage())
				);
	}

}